﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;
using System.Text;
using StoragePeriod.Web.Common;

namespace StoragePeriod.Web.SysManage
{
    public partial class PersonnelRoleManage : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                LoadUserRole();
            }
        }
        /// <summary>
        /// 页面中如果有UpdatePanel控件，需要重写此方法，用于加载页面中js的初始化
        /// </summary>
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            UtilityUI.Init(UpdatePanel1);
        }

        private void BindTreeView()
        {
            this.PersonnelTree.Nodes.Clear();
            using (CMSCateringDataContext data = new CMSCateringDataContext())
            {
                foreach (OperationArea area in data.OperationArea)
                {
                    TreeNode node = new TreeNode()
                    {
                        Value = area.id.ToString(),
                        Text = area.AreaName,
                        ImageUrl = "../images/WebResource.gif",
                        Target = "0",
                        ShowCheckBox = false
                    };
                    foreach (ClassInfo c in data.ClassInfo.Where(p => p.AreaID == area.id))
                    {
                        TreeNode c_node = new TreeNode()
                        {
                            Value = c.id.ToString(),
                            Text = c.CName,
                            ImageUrl = "../images/person.gif",
                            Target = "0",
                            ShowCheckBox = true
                        };
                        foreach (Personnel p in data.Personnel.Where(p => p.ClassID == c.id))
                        {
                            TreeNode p_node = new TreeNode()
                            {
                                Value = p.id.ToString(),
                                Text = p.Name,
                                ImageUrl = "../images/WebResource1.gif",
                                Target = "1",
                                ShowCheckBox = true
                            };
                            c_node.ChildNodes.Add(p_node);
                        }
                        node.ChildNodes.Add(c_node);
                    }
                    this.PersonnelTree.Nodes.Add(node);
                }
            }
        }

        private void LoadUserRole()
        {
            gvRoleView.DataSource = Role.All().Select(p => new { 角色名 = p.RoleName, 角色编码 = p.RoleCode });
            gvRoleView.DataBind();
        }

        #region rub
        /*
        protected void gvprivilegeView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            RolePrivileges.Delete("PrivCode==@0 and RoleCode=@1", e.Keys[0].EToString(), e.Keys[1].EToString());

            Page.Alert(string.Format("权限：{0} 删除成功", e.Values[1].EToString()));
        }

        protected void gvRoleUserView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            RoleUsers.Delete("UserCode==@0 and RoleCode=@1", e.Keys[0].EToString(), e.Keys[1].EToString());

            Page.Alert(string.Format("用户：{0} 删除成功", e.Keys[0].EToString()));
        }
        */
        #endregion

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            string strSearch = this.txt_search.Text.Trim();
            if (strSearch == "") return;
            var res = this.PersonnelTree.Nodes.AllNodes().Where(p => p.Target == "1" && p.Text.Contains(strSearch));

            foreach (TreeNode node in res)
            {
                node.Checked = true;
                node.Parent.Expand();
                node.Parent.Parent.Expand();
            }
        }

        protected void Btn_Select_Click(object sender, EventArgs e)
        {
            txt_Staff.Text = "";
            var resSelectedNodesValue = this.PersonnelTree.CheckedNodes.AllNodes().Where(p => p.Target == "1").Select(p => p.Value);

            resSelectedNodesValue.ToList().ForEach(p => { txt_Staff.Text += p + ";"; });
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            var resSelectedNodesValue = this.PersonnelTree.CheckedNodes.AllNodes().Where(p => p.Target == "1").Select(p => p.Value);

            foreach (GridViewRow item in gvRoleView.Rows)
            {
                RadioButton chose = item.FindControl("chose") as RadioButton;
                if (chose.Checked)
                {

                    resSelectedNodesValue.ToList().ForEach(a =>
                    {
                        //清除选择人物的已有权限
                        RoleUsers.Delete("UserCode==@0", a);

                        //var res = item.Controls.AllOfType<DataControlFieldCell>();
                        var newUserRole = new RoleUsers()
                        {
                            RoleCode = chose.ToolTip,
                            UserCode = a,
                            RoleTypeCode = "1"
                        };

                        RoleUsers.Insert(newUserRole);

                    });
                    UtilityUI.Alert(UpdatePanel1, "角色设置成功");
                    break;
                }
            }
        }

        protected void PersonnelTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            txt_Staff.Text = PersonnelTree.SelectedNode.Text;

            var rolecode = RoleUsers.Where("UserCode==@0", PersonnelTree.SelectedValue).FirstOrDefault();
            if (rolecode == null) return;

            txt_Staff.Text = "角色名：" + Role.Where("RoleCode==@0", rolecode.RoleCode).FirstOrDefault().RoleName;
        }


    }
}